package info.nixbear.tgcms.admin.client.activity;

import info.nixbear.tgcms.admin.client.place.TemplatesPlace;
import info.nixbear.tgcms.admin.client.view.TemplatesView;
import info.nixbear.tgcms.admin.shared.request.AdminRequestFactory;
import info.nixbear.tgcms.admin.shared.request.TemplateProxy;

import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class TemplatesActivity extends AbstractActivity {

	private TemplatesView view;
	private PlaceController placeController;
	private AdminRequestFactory requestFactory;
	
	@Inject
	public TemplatesActivity(
			TemplatesView view, 
			PlaceController placeController,
			AdminRequestFactory requestFactory) {
		this.view = view;
		this.placeController = placeController;
		this.requestFactory = requestFactory;
	}

	public TemplatesActivity withPlace(TemplatesPlace place) {
	    return this;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setTemplatesActivity(this);
		panel.setWidget(view.asWidget());
		requestFactory.templateRequest().listAll().fire(
				new Receiver<List<TemplateProxy>>() {
					@Override
					public void onSuccess(List<TemplateProxy> templates) {
						view.setTemplates(templates);
					}
				});
	}
	
	public void goTo(Place place) {
		placeController.goTo(place);
	}

	@Override
	public void onStop() {
		view.setTemplatesActivity(null);
	}



}
